const { sys_control_type, op } = require("../../middleware/baseModel");

module.exports = {
  "GET /sys_control_type/all": async (ctx, next) => {
    const resList = await sys_control_type.findAll();
    return ctx.success(resList);
  },

  "POST /sys_control_type/page": async (ctx, next) => {
    let param = ctx.getPageSize();
    let row = ctx.getBody();
    let { key, value } = row.seachOption;
    let where = {};
    if (key && value) {
      where[key] = { [op.like]: "%" + value + "%" };
    }

    const res = await sys_control_type.findAndCountAll({
      where,
      order: [["id", "DESC"]],
      ...param,
    });
    return ctx.success(res);
  },

  "POST /sys_control_type/add": async (ctx, next) => {
    let row = ctx.getBody();
    const res = await sys_control_type.create(row);
    return ctx.success(res);
  },
  "POST /sys_control_type/edit": async (ctx, next) => {
    let row = ctx.getBody();
    let id = ctx.get("id");
    const resList = await sys_control_type.update(row, { where: { id: id } });
    return ctx.success(resList);
  },

  "POST /sys_control_type/del": async (ctx, next) => {
    let id = ctx.get("id");
    const res = await sys_control_type.destroy({ where: { id: id } });
    return ctx.success(res);
  },
};
